package org.example.aip;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class OrderService {

    private static Logger logger = LoggerFactory.getLogger(StorageService.class) ;

    //1.采用 Ribbon 的形式
    @Autowired
    private RestTemplate restTemplate ;

    @Autowired
    private OrderServiceFeign orderServiceFeign ;

    //2.采用 Feign 的形式
    public void create(String userId, String commodityCode, int orderCount) {
//        ResponseEntity<Void> entity = restTemplate. getForEntity( "http://order-service/create/{userId}/{commodityCode}/{orderCount}", Void.class, userId , commodityCode, orderCount );
//        if (entity.getStatusCode()== HttpStatus.OK){
//            logger.info("订单创建成功，用户为{} ，商品编号为{}，本次扣减的数量为{}",userId , commodityCode,orderCount);
//            return;
//        }
//        throw new RuntimeException("订单创建失败") ;
        ResponseEntity<Void> entity = orderServiceFeign.create(userId, commodityCode, orderCount);
        if (entity.getStatusCode()== HttpStatus.OK){
            logger.info("订单创建成功，用户为{} ，商品编号为{}，本次扣减的数量为{}",userId , commodityCode,orderCount);
            return;
        }
        throw new RuntimeException("订单创建失败") ;
    }
}
